package org.jeecg.modules.zhima.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.zhima.base.util.ObjectUtils;
import org.jeecg.modules.zhima.base.util.RedisKeyUtil;
import org.jeecg.modules.zhima.base.util.RedisUtil;
import org.jeecg.modules.zhima.center.entity.*;
import org.jeecg.modules.zhima.portal.domain.Category;
import org.jeecg.modules.zhima.portal.domain.PmsPortalProductDetail;
import org.jeecg.modules.zhima.portal.domain.Product;
import org.jeecg.modules.zhima.portal.service.ICategoryService;
import org.jeecg.modules.zhima.portal.service.IProductService;
import org.jeecg.modules.zhima.portal.service.IUserService;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.zhima.center.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: pms_product
 * @Author: jeecg-boot
 * @Date:   2020-08-27
 * @Version: V1.0
 */
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private IUserService userService;
    @Autowired
    private IPmsProductService productService;
    @Autowired
    private IPmsBrandService brandService;
    @Autowired
    private IPmsProductAttributeService pmsProductAttributeService;
    @Autowired
    private IPmsProductAttributeValueService pmsProductAttributeValueService;
    @Autowired
    private IPmsSkuStockService skuStockService;
    @Autowired
    private IPmsProductLadderService productLadderService;
    @Autowired
    private IPmsProductFullReductionService productFullReductionService;
    @Autowired
    private ISmsCouponService couponService;
    @Autowired
    private IPmsCommentReplayService commentReplayService;
    @Autowired
    private IPmsProductCategoryService productCategoryService;
    @Autowired
    private IUmsProductCollectService productCollectService;
    @Autowired
    private IOmsCartItemService cartItemService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IPmsProductHotService productHotService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IUmsSearchHistoryService searchHistoryService;

    @Override
    public PmsPortalProductDetail getDetail(Integer id) {
        String key = redisUtil.getKeyWithColumn(RedisKeyUtil.PRODUCT_DETAIL, String.valueOf(id));
        PmsPortalProductDetail detail;
        if (redisUtil.hasKey(key)){
            detail = (PmsPortalProductDetail)redisUtil.get(key);
            return detail;
        }
        detail = new PmsPortalProductDetail();
        PmsProduct product = productService.getById(id);
        if (product == null){
            return null;
        }
        Product p = new Product();
        BeanUtils.copyProperties(product, p);
        PmsProductHot hot = productHotService.getByProductId(id);
        if (hot != null){
            p.setHot(hot.getHot());
        }
        detail.setProduct(p);
        detail.setBrand(brandService.getById(product.getBrandId()));
        List<PmsProductAttribute> productAttributeList = pmsProductAttributeService.listByCategoryId(product.getProductAttributeCategoryId());
        detail.setProductAttributeList(productAttributeList);
        if (CollectionUtils.isNotEmpty(productAttributeList)){
            List<Integer> ids = productAttributeList.stream().map(PmsProductAttribute::getId).collect(Collectors.toList());
            detail.setProductAttributeValueList(pmsProductAttributeValueService.list(product.getId(), ids));
        }
        detail.setSkuStockList(skuStockService.listByProductId(product.getId()));
        detail.setProductLadderList(productLadderService.listByProductId(product.getId()));
        detail.setProductFullReductionList(productFullReductionService.listByProductId(product.getId()));
        detail.setCommentList(commentReplayService.listByProductId(product.getId()));
        UmsMember umsMember = userService.getCurrentMember();
        if (umsMember != null){
            detail.setCollect(productCollectService.getOne(umsMember.getId(), id));
//            detail.setCartItem(cartItemService.list(id, umsMember.getId()));
        }
        //添加缓存
        redisUtil.set(key, detail);
        return detail;
    }



    @Override
    public List<PmsProduct> listProductsByCategory(Integer categoryId) {
        String key = redisUtil.getKeyWithColumn(RedisKeyUtil.CATEGORY_PRODUCTS, String.valueOf(categoryId));
        List<PmsProduct> productList;
        if (redisUtil.hasKey(key)){
            productList = (List<PmsProduct>)redisUtil.get(key);
            return productList;
        }
        productList = new ArrayList<>(productService.listByCategoryId(categoryId));
        List<Category> categoryList = categoryService.listCategory(categoryId);
        if (CollectionUtils.isNotEmpty(categoryList)){
            categoryList.forEach(category -> {
                productList.addAll(this.listProductsByCategory(category.getId()));
            });
        }
        redisUtil.set(key, productList);
        return productList;
    }

    @Override
    public IPage<Product> pageList(PmsProduct pmsProduct, Integer pageNo, Integer pageSize, QueryWrapper<PmsProduct> queryWrapper) {
        String key = null;
        if (ObjectUtils.objCheckIsNull(pmsProduct)){
            key = redisUtil.getKeyWithColumn(RedisKeyUtil.PRODUCT_LIST, pageNo+"_"+pageSize);
            if (redisUtil.hasKey(key)){
                return (IPage<Product>)redisUtil.get(key);
            }
        }

        if (StringUtils.isNotEmpty(pmsProduct.getKeywords())){
            String keywords = pmsProduct.getKeywords();
            pmsProduct.setKeywords(null);
            queryWrapper.likeRight("keywords", keywords);
            //添加搜索记录
            UmsMember member = userService.getCurrentMember();
            if (member != null){
                UmsSearchHistory searchHistory = new UmsSearchHistory();
                searchHistory.setKeyword(keywords);
                searchHistory.setAddTime(new Date());
                searchHistory.setUserId(member.getId());
                searchHistoryService.save(searchHistory);
            }
        }
        Page<PmsProduct> page = new Page<PmsProduct>(pageNo, pageSize);
        IPage<PmsProduct> pageList = productService.pageList(page, queryWrapper);
        IPage<Product> data = pageList.convert(pmsProduct1 -> {
            Product product = new Product();
            BeanUtils.copyProperties(pmsProduct1, product);
            PmsProductHot hot = productHotService.getByProductId(pmsProduct1.getId());
            if (hot != null){
                product.setHot(hot.getHot());
            }else {
                product.setHot(0);
            }
            return product;
        });
        if (ObjectUtils.objCheckIsNull(pmsProduct)){
            redisUtil.set(key, data);
        }
        return data;
    }


}
